package com.zhanghe.study.springcloudalibaba.controller;


import com.zhanghe.study.springcloud2.model.Dept;
import com.zhanghe.study.springcloud2.response.CommonResult;
import com.zhanghe.study.springcloudalibaba.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zh
 * @date 2021/8/19 12:25
 */
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;
//    @Autowired
//    private DiscoveryClient client;

    @RequestMapping(value = "/dept/add", method = RequestMethod.POST)
    public CommonResult<Boolean> add(@RequestBody Dept dept) {
        deptService.add(dept);
        CommonResult<Boolean> result = new CommonResult<>();
        result.isOk();
        result.setData(true);
        return result;
    }

    @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
    public CommonResult<Dept> get(@PathVariable("id") Long id) {
        Dept dept = deptService.get(id);
        CommonResult<Dept> result = new CommonResult<>();
        result.setData(dept);
        result.isOk();

        return result;
    }

    @RequestMapping(value = "/dept/list", method = RequestMethod.GET)
    public CommonResult<List<Dept>> list() {
        List<Dept> list = deptService.list();
        CommonResult<List<Dept>> result = new CommonResult<>();
        result.setData(list);
        result.isOk();
        return result;
    }

    @GetMapping("/timeout")
    public String timeout(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "完成";
    }

    @GetMapping("/hystrixTest")
    public String hystrixTest(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "完成";
    }


    //	@Autowired
//	private DiscoveryClient client;
//    @RequestMapping(value = "/dept/discovery", method = RequestMethod.GET)
//    public Object discovery()
//    {
//        List<String> list = client.getServices();
//        System.out.println("**********" + list);
//
//        List<ServiceInstance> srvList = client.getInstances("MICROSERVICECLOUD-DEPT");
//        for (ServiceInstance element : srvList) {
//            System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
//                    + element.getUri());
//        }
//        return this.client;
//    }
}
